package com.telenav;

import java.awt.image.BufferedImage;
import java.io.File;
import javax.imageio.ImageIO;

import org.opencv.core.Core;
import org.opencv.core.CvType;
import org.opencv.core.Mat;
import org.opencv.core.Point;
import org.opencv.core.Scalar;
import org.opencv.highgui.Highgui;
import org.opencv.imgproc.Imgproc;

public class Test3 {
	static {
		System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
	}

	/**
	 * @param args
	 * @throws Exception 
	 */
	public static void main(String[] args) throws Exception {
		Mat image = Highgui.imread(new File("data/me.jpg").getAbsolutePath());
		System.out.println(CvType.typeToString(image.type()));
		if(CvType.typeToString(image.type()).equals("CV_8UC3")){
			System.out.println(image.width()+","+image.height());
			BufferedImage bufferImage = new BufferedImage(image.width(), image.height(), BufferedImage.TYPE_INT_BGR);
			for(int j=0;j<bufferImage.getHeight();j++){
				for(int i=0;i<bufferImage.getWidth();i++){
					double[] v = image.get(j, i);
					int pixel = ((int)v[2] << 16) + ((int)v[1] << 8) + (int)v[0];
					bufferImage.setRGB(i, j, pixel);
				}
			}
			ImageIO.write(bufferImage, "BMP", new File("data/me_copy.jpg"));
		}
		Mat dstImage = new Mat(image.height(), image.width(), image.type());
		Mat kernel = new Mat(3,3,CvType.CV_8SC1,new Scalar(0));
		kernel.row(0).col(1).setTo(new Scalar(-1));
		kernel.row(2).col(1).setTo(new Scalar(-1));
		kernel.row(1).col(0).setTo(new Scalar(-1));
		kernel.row(1).col(2).setTo(new Scalar(-1));
		kernel.row(1).col(1).setTo(new Scalar(5));
		Imgproc.filter2D(image, dstImage, image.depth(), kernel);
		Core.circle(dstImage, new Point(300,300), 100, new Scalar(0, 0, 255));
		Highgui.imwrite(new File("data/me_filter.jpg").getAbsolutePath(), dstImage);
	}

}
